package com.century.sjt.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ScrollView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.century.sjt.R;
import com.century.sjt.activity.Pay.TransactionDataActivity;
import com.century.sjt.activity.wallet.WalletActivity;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;
import com.century.sjt.utils.PreferenceManager;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:周玲波
 * @Description:桌面翻译
 * @date : 2016/10/1 01:09
 */

public class TranslationDesktopActivity extends BaseActivity {
    private SjtTitleBar titleBar;
    private WebView mWVcontent;
    private String url;
    private ScrollView layout_scrollview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_desktop_translation);
        initView();
        initEvent();
        initData();
    }

    @Override
    protected void initView() {
        titleBar = (SjtTitleBar) findViewById(R.id.title_bar);
        titleBar.setTitle(getResources().getString(R.string.text_dt_translation));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mWVcontent = (WebView) findViewById(R.id.dt_webview );
        layout_scrollview = (ScrollView) findViewById(R.id.layout_scrollview);

    }

    @Override
    protected void initEvent() {
        WebSettings settings = mWVcontent.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWVcontent.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return false;
            }
        });

    }

    @Override
    protected void initData() {
//        SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
//        String userId = mainSharedPreferences.getString("userId", "");
        Log.e("ll",Constant.translator+"?lan="+ PreferenceManager.getInstance().getLanguageCode());
        mWVcontent.loadUrl(Constant.translator+"?lan="+ PreferenceManager.getInstance().getLanguageCode());

//        gettranslator();
    }



    /**
     *  获取HTML
     */
    private void gettranslator() {
        //显示Progress Dialog
        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), TranslationDesktopActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.translator, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                TipUtil.closeProgressDialog();
                String Html = response.toString();
                mWVcontent.loadDataWithBaseURL("", Html,"text/html","utf-8","");
//                try {
//                    TipUtil.closeProgressDialog();
//                    TipUtil.log("接口成功返回", response.toString(), 1);
//                    JSONTokener jsonParser = new JSONTokener(response);
//                    JSONObject res = (JSONObject) jsonParser.nextValue();
//                    String code = res.getString("code");
//                    String message = res.getString("message");
//                    if (code.equals("407")) {//异地登陆
//                        TipUtil.showAppayRes(TranslationDesktopActivity.this);
//                    } else if (code.equals("200")) {//成功
//                        JSONObject data = res.getJSONObject("data");
//                    } else {//失败
//                        TipUtil.showToast(message, TranslationDesktopActivity.this, 1);
//                    }
//                } catch (Exception ex) {
//                    ex.printStackTrace();
//                    TipUtil.showToast(getResources().getString(R.string.error_service), TranslationDesktopActivity.this, 1);
//                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), TranslationDesktopActivity.this, 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");
                map.put("loginId", userId);
                return map;

            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
//                Log.e("cookie", cookieValue);
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);

    }


}
